<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- /fasttmp/mkdist-qt-4.3.5-1211793125/qtopia-core-opensource-src-4.3.5/doc/src/debug.qdoc -->
<head>
  <title>Qt 4.3: Debugging Techniques</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">Debugging Techniques<br /><small></small></h1>
<p>Here we present some useful hints to help you with debugging your Qt-based software.</p>
<ul><li><a href="#configuring-qt-for-debugging">Configuring Qt for Debugging</a></li>
<ul><li><a href="#mac-os-x-and-xcode">Mac OS X and Xcode</a></li>
</ul>
<li><a href="#command-line-options-recognized-by-qt">Command Line Options Recognized by Qt</a></li>
<li><a href="#warning-and-debugging-messages">Warning and Debugging Messages</a></li>
<li><a href="#providing-support-for-the-qdebug-stream-operator">Providing Support for the qDebug() Stream Operator</a></li>
<li><a href="#debugging-macros">Debugging Macros</a></li>
<li><a href="#common-bugs">Common Bugs</a></li>
</ul>
<a name="configuring-qt-for-debugging"></a>
<h2>Configuring Qt for Debugging</h2>
<p>When <a href="installation.html">configuring Qt for installation</a>, it is possible to ensure that it is built to include debug symbols that can make it easier to track bugs in applications and libraries. However, on some platforms, building Qt in debug mode will cause applications to be larger than desirable.</p>
<a name="mac-os-x-and-xcode"></a>
<h3>Mac OS X and Xcode</h3>
<p>The amount of space taken up by debug symbols generated by GCC can be excessively large. However, with the release of Xcode 2.3 it is now possible to use Dwarf symbols which take up a significantly smaller amount of space. To enable this feature when configuring Qt, pass the <tt>-dwarf-2</tt> option to the configure script.</p>
<p>This is not enabled by default because previous versions of Xcode will not work with the compiler flag used to implement this feature. Mac OS X 10.5 will use dwarf-2 symbols by default.</p>
<p>dwarf-2 symbols contain references to source code, so the size of the final debug application should compare favorably to a release build.</p>
<a name="command-line-options-recognized-by-qt"></a>
<h2>Command Line Options Recognized by Qt</h2>
<p>When you run a Qt application, you can specify several command-line options that can help with debugging. These are recognized by <a href="qapplication.html">QApplication</a>.</p>
<p><table align="center" cellpadding="2" cellspacing="1" border="0">
<thead><tr valign="top" class="qt-style"><th>Option</th><th>Description</th></tr></thead>
<tr valign="top" class="odd"><td><tt>-nograb</tt></td><td>The application should never grab <a href="qwidget.html#grabMouse">the mouse</a> or <a href="qwidget.html#grabKeyboard">the keyboard</a>. This option is set by default when the program is running in the <tt>gdb</tt> debugger under Linux.</td></tr>
<tr valign="top" class="even"><td><tt>-dograb</tt></td><td>Ignore any implicit or explicit <tt>-nograb</tt>. <tt>-dograb</tt> wins over <tt>-nograb</tt> even when <tt>-nograb</tt> is last on the command line.</td></tr>
<tr valign="top" class="odd"><td><tt>-sync</tt></td><td>Runs the application in X synchronous mode. Synchronous mode forces the X server to perform each X client request immediately and not use buffer optimization. It makes the program easier to debug and often much slower. The <tt>-sync</tt> option is only valid for the X11 version of Qt.</td></tr>
</table></p>
<a name="warning-and-debugging-messages"></a>
<h2>Warning and Debugging Messages</h2>
<p>Qt includes four global functions for writing out warning and debug text. You can use them for the following purposes:</p>
<ul>
<li><a href="qtglobal.html#qDebug">qDebug</a>() is used for writing custom debug output.</li>
<li><a href="qtglobal.html#qWarning">qWarning</a>() is used to report warnings and recoverable errors in your application.</li>
<li><a href="qtglobal.html#qCritical">qCritical</a>() is used for writing critical error mesages and reporting system errors.</li>
<li><a href="qtglobal.html#qFatal">qFatal</a>() is used for writing fatal error messages shortly before exiting.</li>
</ul>
<p>If you include the &lt;QtDebug&gt; header file, the <tt>qDebug()</tt> function can also be used as an output stream. For example:</p>
<pre> qDebug() &lt;&lt; &quot;Widget&quot; &lt;&lt; widget &lt;&lt; &quot;at position&quot; &lt;&lt; widget-&gt;pos();</pre>
<p>The Qt implementation of these functions prints the text to the <tt>stderr</tt> output under Unix/X11 and Mac OS X. With Windows, if it is a console application, the text is sent to console; otherwise, it is sent to the debugger. You can take over these functions by installing a message handler using <a href="qtglobal.html#qInstallMsgHandler">qInstallMsgHandler</a>().</p>
<p>If the <tt>QT_FATAL_WARNINGS</tt> environment variable is set, <a href="qtglobal.html#qWarning">qWarning</a>() exits after printing the warning message. This makes it easy to obtain a backtrace in the debugger.</p>
<p>Both <a href="qtglobal.html#qDebug">qDebug</a>() and <a href="qtglobal.html#qWarning">qWarning</a>() are debugging tools. They can be compiled away by defining <tt>QT_NO_DEBUG_OUTPUT</tt> and <tt>QT_NO_WARNING_OUTPUT</tt> during compilation.</p>
<p>The debugging functions <a href="qobject.html#dumpObjectTree">QObject::dumpObjectTree</a>() and <a href="qobject.html#dumpObjectInfo">QObject::dumpObjectInfo</a>() are often useful when an application looks or acts strangely. More useful if you use <a href="qobject.html#objectName-prop">object names</a> than not, but often useful even without names.</p>
<a name="providing-support-for-the-qdebug-stream-operator"></a>
<h2>Providing Support for the qDebug() Stream Operator</h2>
<p>You can implement the stream operator used by <a href="qtglobal.html#qDebug">qDebug</a>() to provide debugging support for your classes. The class that implements the stream is <tt>QDebug</tt>. The functions you need to know about in <tt>QDebug</tt> are <tt>space()</tt> and <tt>nospace()</tt>. They both return a debug stream; the difference between them is whether a space is inserted between each item. Here is an example for a class that represents a 2D coordinate.</p>
<pre> QDebug operator&lt;&lt;(QDebug dbg, const Coordinate &amp;c)
 {
     dbg.nospace() &lt;&lt; &quot;(&quot; &lt;&lt; c.x() &lt;&lt; &quot;, &quot; &lt;&lt; c.y() &lt;&lt; &quot;)&quot;;

     return dbg.space();
 }</pre>
<a name="debugging-macros"></a>
<h2>Debugging Macros</h2>
<p>The header file <tt>&lt;QtGlobal&gt;</tt> contains some debugging macros and <tt>#define</tt>s.</p>
<p>Three important macros are:</p>
<ul>
<li><a href="qtglobal.html#Q_ASSERT">Q_ASSERT</a>(cond), where <tt>cond</tt> is a boolean expression, writes the warning &quot;ASSERT: '<i>cond</i>' in file xyz.cpp, line 234&quot; and exits if <tt>cond</tt> is false.</li>
<li><a href="qtglobal.html#Q_ASSERT_X">Q_ASSERT_X</a>(cond, where, what), where <tt>cond</tt> is a boolean expression, <tt>where</tt> a location, and <tt>what</tt> a message, writes the warning: &quot;ASSERT failure in <tt>where</tt>: '<tt>what</tt>', file xyz.cpp, line 234&quot; and exits if <tt>cond</tt> is false.</li>
<li><a href="qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>(ptr), where <tt>ptr</tt> is a pointer. Writes the warning &quot;In file xyz.cpp, line 234: Out of memory&quot; and exits if <tt>ptr</tt> is 0.</li>
</ul>
<p>These macros are useful for detecting program errors, e.g&#x2e; like this:</p>
<pre> char *alloc(int size)
 {
     Q_ASSERT(size &gt; 0);
     char *ptr = new char[size];
     Q_CHECK_PTR(ptr);
     return ptr;
 }</pre>
<p><a href="qtglobal.html#Q_ASSERT">Q_ASSERT</a>(), <a href="qtglobal.html#Q_ASSERT_X">Q_ASSERT_X</a>(), and <a href="qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>() expand to nothing if <tt>QT_NO_DEBUG</tt> is defined during compilation. For this reason, the arguments to these macro should not have any side-effects. Here is an incorrect usage of <a href="qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>():</p>
<pre> char *alloc(int size)
 {
     char *ptr;
     Q_CHECK_PTR(ptr = new char[size]);  <span class="comment">//</span> WRONG
     return ptr;
 }</pre>
<p>If this code is compiled with <tt>QT_NO_DEBUG</tt> defined, the code in the <a href="qtglobal.html#Q_CHECK_PTR">Q_CHECK_PTR</a>() expression is not executed and <i>alloc</i> returns an unitialized pointer.</p>
<p>The Qt library contains hundreds of internal checks that will print warning messages when a programming error is detected. We therefore recommend that you use a debug version of Qt when developing Qt-based software.</p>
<a name="common-bugs"></a>
<h2>Common Bugs</h2>
<p>There is one bug that is so common that it deserves mention here: If you include the <a href="qobject.html#Q_OBJECT">Q_OBJECT</a> macro in a class declaration and run <a href="moc.html">the meta-object compiler</a> (<tt>moc</tt>), but forget to link the <tt>moc</tt>-generated object code into your executable, you will get very confusing error messages. Any link error complaining about a lack of <tt>vtbl</tt>, <tt>_vtbl</tt>, <tt>__vtbl</tt> or similar is likely to be a result of this problem.</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt 4.3.5</div></td>
</tr></table></div></address></body>
</html>
